// pages/goods/task/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '铺货任务',
            'color': true,
            'class': false
        },
        imgbox: [], //选择图片
        fileIDs: [], //上传云存储后的返回值
        navH: "",
        indicatorDots: true,
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500,
        executorUserId: "",
        pic: "https://pic.localhost.com/",
        winWidth: '',
        winHeight: '',
        yet: 'none',
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        wx.getSystemInfo({
            success: res => {
                this.setData({
                    navH: res.statusBarHeight * (750 / res.windowWidth) + 97,
                    winHeight: res.windowHeight,
                    winWidth: res.windowWidth,
                    tabbar: (res.screenHeight - res.windowHeight - res.statusBarHeight) * res.pixelRatio
                })
            }
        });
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                that.setData({
                    executorUserId: res.data.id,
                    navH: app.globalData.navHeight
                })
                that.orderInfo(options);
            }
        })
    },
    signIn() {
        var userpass = wx.getStorageSync('userpass')
        var data = {
            password: userpass.password,
            username: userpass.username
        }
        app.basePost(app.U({ 
            m: 'factory', 
            c: 'sysuser', 
            a: 'login' 
        }), data, res => {
            if (res.code == 200) {
                wx.setStorage({
                    key: 'token', //key名
                    data: res.data.token //值
                })
            } else {
                wx.showToast({
                    title: "登陆过期",
                })
                wx.navigateTo({
                    url: '/pages/logs/logs'
                })
            }
        });
    },
    //任务执行铺货
    orderInfo(options) {
        var that = this;
        app.baseGet(app.U({
            m: 'api',
            c: 'task',
            a: 'worker/distribution/getOrder',
            q: {
                goodsId: options.goodsId,
                dealerId: options.dealerId,
                userId: that.data.executorUserId
            }
        }), res => {
            that.setData({
                orderinfo: res.data,
                status: res.data.status,
                dealer: res.data.dealer,
                goods: res.data.goods,
                fileIDs: res.data.photoUrl
            })
        }, (res) => {})
    },
    // 删除照片 &&
    imgDelete1: function (e) {
        let that = this;
        let index = e.currentTarget.dataset.deindex;
        let imgbox = this.data.imgbox;
        let fileIDs = this.data.fileIDs;
        imgbox.splice(index, 1)
        fileIDs.splice(index, 1)
        that.setData({
            imgbox: imgbox,
            fileIDs: fileIDs
        });
    },
    // 选择图片 &&&
    addPic1: function (e) {
        var that = this;
        var imgbox = this.data.imgbox;
        wx.chooseImage({
            count: 1, // 默认9，设置图片张数
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                var tempFilePaths = res.tempFilePaths,
                    fileIDs = that.data.fileIDs ? that.data.fileIDs : [],
                    image = imgbox.concat(tempFilePaths)
                that.setData({
                    imgbox: image
                });
                var url = app.U({
                    m: 'api',
                    c: 'task',
                    a: 'worker/distribution/upload',
                })
                wx.showLoading({
                    title: '上传中',
                })
                wx.uploadFile({
                    url: url,
                    filePath: that.data.imgbox[that.data.imgbox.length - 1],
                    name: 'file',
                    formData: {
                        'filename': 'pics',
                    },
                    header: {
                        "Content-Type": "multipart/form-data",
                        authToken: wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
                        requestTerminal: 20
                    },
                    success: function (res) {
                        if (res.statusCode == 200) {
                            that.setData({
                                fileIDs: fileIDs.concat(res.data)
                            })
                            wx.hideLoading();
                            wx.showToast({
                                title: "上传成功",
                            })
                        } else {
                            that.signIn();
                            wx.hideLoading();
                            wx.showToast({
                                title: "上传失败",
                            })
                        }
                    },
                    fail: function (res) {
                        wx.hideLoading();
                        wx.showToast({
                            title: "上传失败",
                        })
                    }
                })
            }
        })
    },
    // 发布按钮
    fb: function (e) {
        var that = this;
        var files = that.data.fileIDs
        var url = app.U({
            m: 'api',
            c: 'task',
            a: 'worker/distribution/successOrder',
            q: {
                goodsId: that.data.goods.goodsId,
                dealerId: that.data.dealer.dealerId,
                userId: that.data.executorUserId,
                files: files
            }
        })
        wx.request({
            url: url,
            method: 'POST',
            header: {
                // "Content-Type": "multipart/form-data",
                authToken: wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
                requestTerminal: 20
            },
            success: function (res) {
                wx.showToast({
                    title: "铺货成功",
                })
                that.back()
            },
            fail: function (res) {
                console.log(res)
            }
        })
    },
    back: function () {
        wx.navigateBack();
    },
    cancle: function (e) {
        this.setData({
            yet: this.data.yet == "none" ? "block" : 'none'
        })
    }
})